package com.aspirecn.core.commons;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.aspirecn.core.ShareCommon;

/**
 * sharecommon内部常量
 * @author wangxinning
 *
 */
public class Configuration {

	public final static Logger logger = Logger.getLogger(Configuration.class);
	
	private static Configuration instance=new Configuration();
	
	private PropertiesConfiguration config = null;
	
	private Configuration()
	{
		try {
			config = new PropertiesConfiguration("sharecommon.properties");
			//此处可以添加监听器，实现动态更新配置文件内容
			config.setReloadingStrategy(new FileChangedReloadingStrategy());
		} catch (ConfigurationException e) {
			logger.error("sharecommon.properties文件加载失败", e);
			e.printStackTrace();
		}
	}
	
	public static Configuration getInstance() {
		return instance;
	}

	//获取下载文件本地保存路径
	public String getDownloadFilePath()
	{
		String downloadPath = null;
		if( config != null )
		{
			downloadPath = config.getString("download.file.path",
					FileUtils.getTempDirectoryPath());
		}
		else
		{
			downloadPath = FileUtils.getTempDirectoryPath();
		}				
		
		if (StringUtils.equals(downloadPath, "${java.io.tmpdir}")) {
			downloadPath = FileUtils
					.getTempDirectoryPath();
		}
		
		return downloadPath;
	}
	
	//获取远程索引文件地址
	public String getDownloadIndexUrl()
	{
		String downloadIndexUrl = null;
		
		if( config != null )
		{
			downloadIndexUrl = config.getString("download.index.url",
					"http://cfg.api.i139.cn/common/FullIndex.xml");
		}
		else
		{
			downloadIndexUrl = "http://cfg.api.i139.cn/common/FullIndex.xml";
		}	
				
		return downloadIndexUrl;
	}
	
	
	//短地址服务
	public String getShorturlUrl()
	{
		String shorturlUrl = null;
		
		if( config != null )
		{
			shorturlUrl = config.getString("client.shorturl.url",
					"http://j.i139.cn/jv2/");
		}
		else
		{
			shorturlUrl = "http://j.i139.cn/jv2/";
		}
		
		
		return shorturlUrl;
	}
	
	//IP表达式，先从xml获取内容
	public String getIPExpression()
	{
		//1.首先从缓存中获取
		String ipExpression = ShareCommon.getInstance().getShareCommonCache().get("ip.expression");
		if( StringUtils.isBlank(ipExpression) )
		{
			ipExpression = config.getString("ip.expression",
					"[value=(RemoteAddr)]");//默认取RemoteAddr
		}
				
		return ipExpression;
	}
	
	
	public String[] getXssNoCheckHost()
	{
		String[] retArray = null;
		String nocheckhost = ShareCommon.getInstance().getShareCommonCache().get("xss.nocheck.host");
		
		if( StringUtils.isBlank(nocheckhost) )
		{
			nocheckhost = "[10086.cn][i139.cn][mmarket.com]";
		}
		
		if( StringUtils.isNotBlank(nocheckhost) )
		{
			retArray = StringUtils.substringsBetween(nocheckhost, "[", "]");
		}
		
		return retArray;
	}
	
	
	/**
	 * 包含下面的关键词，一个关键字适配也成立
	 * @return
	 */
	public String[] getUAMatchOneKeyword()
	{
		String[] retArray = null;
		String uamatchOneKeyword = ShareCommon.getInstance().getShareCommonCache().get("ua.match.onekeyword");
		
		if( StringUtils.isBlank(uamatchOneKeyword) )
		{
			uamatchOneKeyword = "[ucweb]";
		}
		
		if( StringUtils.isNotBlank(uamatchOneKeyword) )
		{
			retArray = StringUtils.substringsBetween(uamatchOneKeyword, "[", "]");
		}
		
		return retArray;
	}
	
	
	
	public String[] getUAMatchSuper()
	{
		String[] retArray = null;
		String uamatchOneKeyword = ShareCommon.getInstance().getShareCommonCache().get("ua.match.supermatch");
		
		if( StringUtils.isNotBlank(uamatchOneKeyword) )
		{
			retArray = StringUtils.substringsBetween(uamatchOneKeyword, "[", "]");
		}
		
		return retArray;
	}
	
	
	
	
	
	
	//是否调试模式
	public boolean isDebug()
	{
		return config.getBoolean("isDebug", true);
	}
	
	//下载文件轮询时间
	public int getRollMinute()
	{
		int waittime = config.getInt(
				"download.file.roll.minute", 5);		
		return waittime;
	}
	
	//是否采用ForMMTest获取IP，先从xml获取内容
	public boolean useForMMTest()
	{
		//1.首先从缓存中获取
		String useForMMTest = ShareCommon.getInstance().getShareCommonCache().get("ip.useForMMTest");
		if( StringUtils.isBlank(useForMMTest) )
		{
			return config.getBoolean("ip.useForMMTest", true);
		}
		else
		{
			if( StringUtils.equals(useForMMTest, "true") )
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	
	
	public boolean isMM()
	{
		return config.getBoolean("uaFile.isMM", false);
	}
	
	
	public boolean switchIPCache() {
		return StringUtils.equals(config.getString("cache.ip.switch", "on"),
				"on") ? true : false;
	}

	public boolean switchGEOCache() {
		return StringUtils.equals(config.getString("cache.geo.switch", "on"),
				"on") ? true : false;
	}

	public boolean switchXSSCache() {
		return StringUtils.equals(config.getString("cache.xss.switch", "on"),
				"on") ? true : false;
	}
	
	public boolean switchUACache() {
		return StringUtils.equals(config.getString("cache.ua.switch", "on"),
				"on") ? true : false;
	}
	
	public boolean switchHaoduanCache() {
		return StringUtils.equals(config.getString("cache.haoduan.switch", "on"),
				"on") ? true : false;
	}
	
	public boolean switchStockCache() {
		return StringUtils.equals(config.getString("cache.stock.switch", "on"),
				"on") ? true : false;
	}
}